﻿using System;
using System.Drawing;
using System.IO;
using System.Net;

namespace ZYCommon
{
   
    public class CommonFunction
    {
        public static Color ConvertToColor(int intColor)
        {
            return Color.FromArgb((intColor & 0xff0000) >> 0x10, (intColor & 0xff00) >> 8, intColor & 0xff);
        }

        public static bool GetIntAttribute(int intAttributes, int intValue)
        {
            return ((intAttributes & intValue) == intValue);
        }

        public static byte[] HttpPostData(string strURL, byte[] bytSend, ProgressHandler SendProgress, ProgressHandler AcceptProgress)
        {
            HttpWebRequest myReq = (HttpWebRequest) WebRequest.Create(strURL);
            myReq.Method = "POST";
            Stream myStream = myReq.GetRequestStream();
            int iCount = 0;
            if (SendProgress != null)
            {
                SendProgress(0, bytSend.Length);
            }
            while (iCount < bytSend.Length)
            {
                if ((iCount + 0x400) > bytSend.Length)
                {
                    myStream.Write(bytSend, iCount, bytSend.Length - iCount);
                    iCount = bytSend.Length;
                }
                else
                {
                    myStream.Write(bytSend, iCount, 0x400);
                    iCount += 0x400;
                }
                if (SendProgress != null)
                {
                    SendProgress(iCount, bytSend.Length);
                }
            }
            if (SendProgress != null)
            {
                SendProgress(bytSend.Length, bytSend.Length);
            }
            myStream.Close();
            HttpWebResponse myRes = null;
            myRes = myReq.GetResponse() as HttpWebResponse;
            myStream = myRes.GetResponseStream();
            MemoryStream myBuf = new MemoryStream(0x400);
            byte[] bytBuf = new byte[0x400];
            int ContentLength = (int) myRes.ContentLength;
            int AcceptLength = 0;
            if (AcceptProgress != null)
            {
                AcceptProgress(0, ContentLength);
            }
            while (true)
            {
                do
                {
                    int iLen = myStream.Read(bytBuf, 0, 0x400);
                    if (iLen == 0)
                    {
                        goto Label_0114;
                    }
                    myBuf.Write(bytBuf, 0, iLen);
                    AcceptLength += iLen;
                    if (AcceptLength > ContentLength)
                    {
                        ContentLength = AcceptLength;
                    }
                }
                while (AcceptProgress == null);
                AcceptProgress(AcceptLength, ContentLength);
            }
        Label_0114:
            if (AcceptProgress != null)
            {
                AcceptProgress(AcceptLength, ContentLength);
            }
            myStream.Close();
            myRes.Close();
            myReq.Abort();
            byte[] bytReturn = myBuf.ToArray();
            myBuf.Close();
            return bytReturn;
        }

        public static int SetIntAttribute(int intAttributes, int intValue, bool bolSet)
        {
            return (bolSet ? (intAttributes | intValue) : (intAttributes & ~intValue));
        }
    }
}

